﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using ExitGames.Client.Photon;
using ExitGames.Client.Photon.Lite;
using DICE_GS;

public class EvolutionSceneManager : LTEventListenerC
{
    public MyUnitCard m_SourceCard;
    public MyUnitCard m_MeterialCard;

    public UILabel m_CrrNum;
    public UILabel m_NeedNum;
    public UILabel m_XCrrNum;
    public UILabel m_NeedCardName;
    public UILabel m_Title;
    public UILabel m_Body;
    public UILabel m_NeedMoney;

    public GameObject m_LevelUpBtn;
    public GameObject m_GoalCardRoot;
    public MyUnitCard m_GoalCard;

    List<UnitInfo> m_Materials = new List<UnitInfo>();


    enum Mode_Evolution
    {
        initial,
        setting
    }

    Mode_Evolution m_Mode = Mode_Evolution.initial;

    void UpdateUI()
    {
        //m_Title.text = m_MeterialCard.;
        m_Body.text = "";
        m_NeedMoney.text = "0";
    }

    void CallBackOfRefreshMyUnitList(Hashtable returnData)
    {
        if (DeckCardSelectSceneManager.SelectCardInfos.Count != 0)
        {
            m_SourceCard.gameObject.SetActive(true);
            m_MeterialCard.gameObject.SetActive(true);

            m_SourceCard.SetCardInfo(0, DeckCardSelectSceneManager.SelectCardInfos[0]);
            CUnitData crrCard = CUnitDataManager.Instance.GetUnitData(m_SourceCard._unitInfo.UnitClassID);

            CUnitData goalCard = CUnitDataManager.Instance.GetUnitData(crrCard.evolution_target);


            if (goalCard == null) // 진화 불가능
            {
                m_MeterialCard.gameObject.SetActive(false);

                m_CrrNum.text = "";
                m_NeedNum.text = "";
                m_NeedCardName.text = ""; ;
                m_XCrrNum.text = "";
                m_Title.text = "";
                m_Body.text = "";
                m_NeedMoney.text = "0";
                ButtonDisable(m_LevelUpBtn, false);
            }
            else   // 진화 가능
            {
                CUnitAbilityItem plusData = CUnitAbilityDataManager.Instance.GetUnitAbilityData("1_" + goalCard.unit_class);

                CSkillData skillData = CSkillDataManager.Instance.FindSkillData(goalCard.passive_skill);

                Dictionary<int, CRecipeData> tmp = CRecipeDataManager.Instance.GetRecipeMap(element.sky);

                CRecipeData RD = CRecipeDataManager.Instance.GetRecipeData(m_SourceCard._unitInfo.element, m_SourceCard._unitInfo.grade);

                int needNum = 0;
                int needGold = 0;
                string material = RD.ingredient_card;
                CUnitData materials = CUnitDataManager.Instance.GetUnitData(material);

                switch (crrCard.evolution_step)
                {
                    case 1:
                        needNum = RD.evol_1_card;
                        needGold = RD.evol_1_gold;
                        break;
                    case 2:
                        needNum = RD.evol_2_card;
                        needGold = RD.evol_2_gold;
                        break;
                    case 3:
                        needNum = RD.evol_3_card;
                        needGold = RD.evol_3_gold;
                        break;
                }

                int crrCardNum = 0;

                m_Materials.Clear();
                foreach (UnitInfo t in MyInfo.Instance.MyUnitsList)
                {
                    CUnitData tCard = CUnitDataManager.Instance.GetUnitData(t.UnitClassID);

                    if (tCard.name == material && m_SourceCard._unitInfo != t) // 내가 아닌 속성만 같은 카드
                    {
                        if (crrCardNum < needNum)
                            m_Materials.Add(t);

                        crrCardNum++;
                    }
                }

                m_MeterialCard.gameObject.SetActive(true);
                m_MeterialCard.SetCardInfo(0, 1, goalCard, plusData);

                m_CrrNum.text = crrCardNum.ToString();
                m_NeedNum.text = needNum.ToString();
                m_NeedCardName.text = materials.name_kor;
                m_XCrrNum.text = string.Format("x{0}", crrCardNum);
                m_Title.text = CStringTableManager.Instance.GetItemByName(skillData.name_string);
                m_Body.text = CStringTableManager.Instance.GetItemByName(skillData.desc_string);
                m_NeedMoney.text = needGold.ToString();

                if (crrCardNum >= needNum && MyInfo.Instance.Money >= needGold && m_SourceCard._unitInfo.UnitLevel == crrCard.max_lv)
                {
                    ButtonDisable(m_LevelUpBtn, true);
                }
                else
                {
                    ButtonDisable(m_LevelUpBtn, false);

                    //if (m_SourceCard._unitInfo.UnitLevel != crrCard.max_lv)
                    //    m_NeedCardName.text = "[cc0000]카드의 레벨이 부족합니다";
                    //else if (crrCardNum < needNum)
                    //    m_NeedCardName.text = "[cc0000]재료카드가 부족 합니다";
                    //else if (MyInfo.Instance.Money < needGold)
                    //    m_NeedCardName.text = CStringTableManager.Instance.GetItemById(2295);
                }
            }
        }
        else
        {
            m_SourceCard.gameObject.SetActive(false);
            m_MeterialCard.gameObject.SetActive(false);

            m_CrrNum.text = "";
            m_NeedNum.text = "";
            m_NeedCardName.text = "";
            m_XCrrNum.text = "";
            m_Title.text = "";
            m_Body.text = "";
            m_NeedMoney.text = "0";
            ButtonDisable(m_LevelUpBtn, false);
        }

        DeckCardSelectSceneManager.SelectCardInfos.Clear();  //선택씬 초기화
    }


    void Awake()
    {
        m_MeterialCard.gameObject.SetActive(false);

        m_CrrNum.text = "";
        m_NeedNum.text = "";
        m_NeedCardName.text = ""; ;
        m_XCrrNum.text = "";
        m_Title.text = "";
        m_Body.text = "";
        m_NeedMoney.text = "0";
        ButtonDisable(m_LevelUpBtn, false);

        LoadingWindow.window.ShowShortLoading();
        RestAPI.singleton.RefreshUserData(CallBackOfRefreshUserData);
    }

    void CallBackOfRefreshUserData(Hashtable returnData)
    {
        LoadingWindow.window.HideLoading();
        RestAPI.singleton.GetMyUnitList(CallBackOfRefreshMyUnitList);
    }


    void OnClickedBackButton()
    {
        CSceneManager.manager.GoToWaitroomScene();
    }

    void OnLevelUp()
    {
        RestAPI.singleton.SendUnitEvolutionResult(SendUnitEvolutionResultReturnCallback, m_SourceCard._unitInfo.UnitDBID, m_Materials);
    }

    void SendUnitEvolutionResultReturnCallback(Hashtable returnData)
    {
        //m_MeterialCard.transform.parent = m_GoalCard.transform.parent;
        //m_MeterialCard.transform.localPosition = m_GoalCard.transform.localPosition;
        //m_MeterialCard.transform.localScale = m_GoalCard.transform.localScale;
        //Destroy(m_GoalCard.gameObject);
        //m_GoalCardRoot.SetActive(true);

        RestAPI.singleton.RefreshMyDeckInfo(ReturnCallback);
    }

    void ReturnCallback(Hashtable returnData)
    {
        CSceneManager c = GameObject.FindObjectOfType(typeof(CSceneManager)) as CSceneManager;
        c.GoToSummonCommonScene();

        CUnitData crrCard = CUnitDataManager.Instance.GetUnitData(m_SourceCard._unitInfo.UnitClassID);
        CUnitData goalCard = CUnitDataManager.Instance.GetUnitData(crrCard.evolution_target);

        CharacterSummonManager.isSummonFlow = false;
        CharacterSummonManager.characterId = goalCard.id;

       // CSceneManager.manager.GoToUnitPowerUpScene();
    }

    //void OnGoalButton()
    //{
    //    RestAPI.singleton.RefreshMyDeckInfo(ReturnCallback);
    //}

    void OnSlot1(GameObject obj)
    {
        DeckCardSelectSceneManager.isMultySelect = false;
        DeckCardSelectSceneManager.OrizinCard = null;
        DeckCardSelectSceneManager.isEnhance = false;
        CSceneManager.manager.GoToSelectUnitScene();

        //m_Mode = Mode_Evolution.setting;
    }

    void ButtonDisable(GameObject obj, bool isActive)
    {
        obj.collider.enabled = isActive;

        MultiplySpriteColor sc = obj.GetComponent<MultiplySpriteColor>();
        if (sc == null)
            sc = obj.AddComponent<MultiplySpriteColor>();

        if (isActive)
            sc.enabled = !isActive;
        else
        {
            sc.MulEndColor = Color.gray;
            sc.Apply(true);
        }
    }
}
